package one.four.two;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.Stopwatch;

public class ThreeSumForBigInt {

	private ThreeSumForBigInt() {
	}

	public static int count(BigInt[] a) {
		int n = a.length;
		int count = 0;
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = j + 1; k < n; k++) {
					BigInt mid = a[i].add(a[j]);
					BigInt sum = mid.add(a[k]);
					if (sum.zero()) {
						count++;
					}
				}
			}
		}
		return count;
	}

	/**
	 * Reads in a sequence of integers from a file, specified as a command-line
	 * argument; counts the number of triples sum to exactly zero; prints out the
	 * time to perform the computation.
	 *
	 * @param args the command-line arguments
	 */
	public static void main(String[] args) {

		In in = new In("n.txt"); // In in = new In(args[0]);
		String[] s = in.readAllStrings();
		//System.out.println(Arrays.toString(s));
		
		BigInt[] b=new BigInt[s.length];
		for(int i=0;i<s.length;i++) {
			b[i]=new BigInt(s[i]);
		}
		Stopwatch timer = new Stopwatch();
		int count = count(b);
		StdOut.println("elapsed time = " + timer.elapsedTime());
		StdOut.println(count);
		
	}
}
